package linuxbox.status.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.status.logic.*;

/**
 * This class displays a department list.
 */
public class StatusOptionList extends TagSupport {
  private static final String LIST_TYPE_LIST = "LIST";

  protected String listType = null;
  protected int selected = -1;

  public int doStartTag() throws JspException {
    try {
      Map opts = StatusManager.getStatusOptions();
      JspWriter out = pageContext.getOut();
      if (opts == null) return SKIP_BODY;

      if (LIST_TYPE_LIST.equalsIgnoreCase(listType)) {
        StringBuffer retVal = new StringBuffer();
        for (Iterator it = opts.keySet().iterator();
             it.hasNext(); ) {
          Integer id = (Integer)it.next();
          String str = (String)opts.get(id);

          retVal.append("<OPTION value=" + id.intValue());
          if (selected == id.intValue())
            retVal.append(" selected ");
          retVal.append(">" + str + "</OPTION>");
        }
        out.println(retVal.toString());
      }

      out.flush();
    } catch(Exception e) {
      BoxStatic.logger.debug("Failed to get all users");
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setListType(String aValue) {
    listType = aValue;
  }

  public void setSelected(int aValue) {
     selected = aValue;
   }
}